//
//  WMyViewController.swift
//  iiatn
//
//  Created by mnine on 18/4/1.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import Alamofire
import SVProgressHUD
import SwiftyJSON

class WMyViewController: itBaseViewController{

    @IBOutlet weak var bgimage: UIImageView!
    
    @IBOutlet weak var headimage: UIImageView!
    
    @IBOutlet weak var loginbtn: UIButton!
    
    @IBOutlet var shuoming: UILabel!
    
    @IBOutlet weak var scbtn: UIButton!
    
    @IBOutlet var sczxbtn: UIButton!
    
    @IBOutlet weak var fbbtn: UIButton!
    
    @IBOutlet weak var infobtn: UIButton!
    
    @IBOutlet weak var versionbtn: UIButton!
    
    @IBOutlet var versioninfo: UILabel!
    
    @IBOutlet weak var mloginbtn: UIButton!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        view.backgroundColor = WLightWhiteColor()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Slider_18x18_"), style: .plain, target: self, action: #selector(LeftBBClick))
        
        NotificationCenter.default.addObserver(self, selector: #selector(notificationName), name: notifyNameMsgRecv, object: nil)
        loadsth()
        hide()
        hidesth()
        dosth()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func hidesth(){
        let url = BASE_URL + "apin/version"
        Alamofire
            .request(url)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "检查失败...")
                    return
                }
                if let value = response.result.value {
                    let dict = JSON(value)
                    let status = dict["status"].intValue
                    let off = dict["off"].intValue
                    if status == RETURN_OK {
                        if off == 0 {
                            self.present()
                        }
                    }
                }
        }
    }
    func hide(){
        versionbtn.isHidden = true
        versioninfo.isHidden = true
        mloginbtn.y = 485
    }
    func present(){
        versionbtn.isHidden = false
        versioninfo.isHidden = false
        mloginbtn.y = 523
    }
    
    func dosth(){
        loginbtn.addTarget(self, action: #selector(toinfo), for: .touchUpInside)
        scbtn.addTarget(self, action: #selector(tomysc), for: .touchUpInside)
        sczxbtn.addTarget(self, action: #selector(tomysczx), for: .touchUpInside)
        fbbtn.addTarget(self, action: #selector(tomyfb), for: .touchUpInside)
        infobtn.addTarget(self, action: #selector(toinfo), for: .touchUpInside)
        versionbtn.addTarget(self, action: #selector(toversion), for: .touchUpInside)
        mloginbtn.addTarget(self, action: #selector(tologin), for: .touchUpInside)
    }
    func toversion(){
        let url = BASE_URL + "apin/version"
        Alamofire
            .request(url)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "检查失败...")
                    return
                }
                if let value = response.result.value {
                    let dict = JSON(value)
                    let status = dict["status"].intValue
                    let msg = dict["msg"].stringValue
                    let update_url = dict["update_url"].stringValue
                    let version = dict["version"].floatValue
                    if status == RETURN_OK {
                        let infoDic = Bundle.main.infoDictionary
                        let infodic = JSON(infoDic!)
                        let appVersion = infodic["CFBundleShortVersionString"].floatValue
                        //let sysUUID = UIDevice.current.identifierForVendor
                        //print(update_url,version,appVersion)
                        if version == appVersion {
                            SVProgressHUD.showInfo(withStatus: msg + " 已是最新版本!")
                        }else {
                            self.versioninfo.text = "（ 已有新版本！）"
                            
                            let alertController = UIAlertController(title: "版本更新",
                                                                    message: "当前APP最新版本为：ios \(version)，是否立即更新下载？", preferredStyle: .alert)
                            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
                            let okAction = UIAlertAction(title: "确认", style: .default, handler: {
                                action in
                                self.godownload(url: update_url)
                                
                            })
                            alertController.addAction(cancelAction)
                            alertController.addAction(okAction)
                            self.present(alertController, animated: true, completion: nil)
                        }
                        
                    }
                }
        }
    }
    func godownload(url: String){
        print(url)
        // 例子网易云
        UIApplication.shared.openURL(URL(string:url)!)
    }
    
    func loadsth(){
        if whaslogin! {
            mloginbtn.setTitle("    切换账号", for: .normal)
            loginbtn.setTitle(wname,for: .normal)
        }else {
           print("wname :nil")
        }
        
        headimage.x = SCREENW/2 - headimage.width/2
        loginbtn.x = SCREENW/2 - headimage.width/2
        
        shuoming.width = SCREENW - shuoming.x*2
        bgimage.width = SCREENW
        fbbtn.width = SCREENW
        scbtn.width = SCREENW
        sczxbtn.width = SCREENW
        infobtn.width = SCREENW
        versionbtn.width = SCREENW
        mloginbtn.width = SCREENW
        
        
        bgimage.image = UIImage(named: "Me_ProfileBackground")
        headimage.image = UIImage(named: "Me_AvatarPlaceholder1_75x75_")
        headimage.layer.cornerRadius = headimage.width * 0.5
        headimage.layer.masksToBounds = true
        
        // 边框
        scbtn.layer.borderWidth = 1.0
        sczxbtn.layer.borderWidth = 1.0
        fbbtn.layer.borderWidth = 1.0
        infobtn.layer.borderWidth = 1.0
        versionbtn.layer.borderWidth = 1.0
        mloginbtn.layer.borderWidth = 1.0
        scbtn.layer.borderColor = UIColor.gray.cgColor
        sczxbtn.layer.borderColor = UIColor.gray.cgColor
        fbbtn.layer.borderColor = UIColor.gray.cgColor
        infobtn.layer.borderColor = UIColor.gray.cgColor
        versionbtn.layer.borderColor = UIColor.gray.cgColor
        mloginbtn.layer.borderColor = UIColor.gray.cgColor
    }
    
    func tomysc(){
        if whaslogin!{
            let detailVC = WProscViewController()
            detailVC.title = "项目收藏"
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            tologin()
        }
    }
    
    func tomysczx(){
        if whaslogin!{
            let detailVC = WZXscViewController()
            detailVC.title = "资讯收藏"
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            tologin()
        }
    }
    
    func tomyfb(){
        if whaslogin!{
            let detailVC = WProfbViewController()
            detailVC.title = "我的发布"
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            tologin()
        }
    }
    
    func toinfo(){
        if whaslogin!{
            let detailVC = WInfoViewController()
            detailVC.title = "个人信息"
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            tologin()
        }
    }
    func tologin(){
        let loginVC = WLoginViewController()
        loginVC.title = "登录"
        let nav = WNavigationController(rootViewController: loginVC)
        present(nav, animated: true, completion: nil)
    }
    
    
    // 实际上就是登录成功之后的一些操作，广播之后的操作,需要修改名字
    func notificationName()
    {
        loginbtn.setTitle(wname!,for: .normal)
        mloginbtn.setTitle("    切换账号", for: .normal)
    }

}
